extends Node

# 主菜单脚本
# 负责处理游戏主菜单界面和交互

# 引用
var game_optimizer

func _ready():
	# 获取游戏优化器引用
	game_optimizer = get_tree().get_first_node_in_group("game_optimizer")
	
	# 如果没有游戏优化器，创建一个
	if not game_optimizer:
		game_optimizer = load("res://GameOptimizer.gd").new()
		game_optimizer.name = "GameOptimizer"
		add_child(game_optimizer)
		game_optimizer.add_to_group("game_optimizer")
	
	# 播放主菜单音乐
	if game_optimizer:
		game_optimizer.play_music("main_menu")
	
	# 连接按钮信号
	$StartButton.connect("pressed", _on_start_button_pressed)
	$SettingsButton.connect("pressed", _on_settings_button_pressed)
	$QuitButton.connect("pressed", _on_quit_button_pressed)
	
	# 设置面板
	$SettingsPanel.visible = false
	$SettingsPanel/CloseButton.connect("pressed", _on_settings_close_button_pressed)
	$SettingsPanel/MasterVolumeSlider.connect("value_changed", _on_master_volume_changed)
	$SettingsPanel/MusicVolumeSlider.connect("value_changed", _on_music_volume_changed)
	$SettingsPanel/SFXVolumeSlider.connect("value_changed", _on_sfx_volume_changed)
	
	# 初始化设置面板
	if game_optimizer:
		$SettingsPanel/MasterVolumeSlider.value = game_optimizer.master_volume
		$SettingsPanel/MusicVolumeSlider.value = game_optimizer.music_volume
		$SettingsPanel/SFXVolumeSlider.value = game_optimizer.sfx_volume
	
	# 添加版本信息
	var version_label = Label.new()
	version_label.text = "v1.0.0"
	version_label.position = Vector2(10, get_viewport_rect().size.y - 30)
	add_child(version_label)
	
	# 添加标题动画
	animate_title()

func _process(delta):
	# 处理输入
	if Input.is_action_just_pressed("ui_cancel") and $SettingsPanel.visible:
		_on_settings_close_button_pressed()

func animate_title():
	# 获取标题标签
	var title_label = $TitleLabel
	
	# 创建动画
	var tween = create_tween()
	tween.set_loops()
	
	# 缩放动画
	tween.tween_property(title_label, "scale", Vector2(1.1, 1.1), 1.0)
	tween.tween_property(title_label, "scale", Vector2(1.0, 1.0), 1.0)

func _on_start_button_pressed():
	# 播放按钮音效
	if game_optimizer:
		game_optimizer.play_sound("button_click")
	
	# 显示难度选择面板
	$DifficultyPanel.visible = true
	$DifficultyPanel/EasyButton.connect("pressed", _on_difficulty_selected.bind(1))
	$DifficultyPanel/NormalButton.connect("pressed", _on_difficulty_selected.bind(2))
	$DifficultyPanel/HardButton.connect("pressed", _on_difficulty_selected.bind(3))
	$DifficultyPanel/BackButton.connect("pressed", _on_difficulty_back_button_pressed)

func _on_settings_button_pressed():
	# 播放按钮音效
	if game_optimizer:
		game_optimizer.play_sound("button_click")
	
	# 显示设置面板
	$SettingsPanel.visible = true

func _on_quit_button_pressed():
	# 播放按钮音效
	if game_optimizer:
		game_optimizer.play_sound("button_click")
	
	# 退出游戏
	await get_tree().create_timer(0.3).timeout
	get_tree().quit()

func _on_settings_close_button_pressed():
	# 播放按钮音效
	if game_optimizer:
		game_optimizer.play_sound("button_click")
	
	# 隐藏设置面板
	$SettingsPanel.visible = false

func _on_master_volume_changed(value):
	if game_optimizer:
		game_optimizer.set_master_volume(value)

func _on_music_volume_changed(value):
	if game_optimizer:
		game_optimizer.set_music_volume(value)

func _on_sfx_volume_changed(value):
	if game_optimizer:
		game_optimizer.set_sfx_volume(value)
		
		# 播放测试音效
		game_optimizer.play_sound("button_click")

func _on_difficulty_selected(difficulty):
	# 播放按钮音效
	if game_optimizer:
		game_optimizer.play_sound("button_click")
	
	# 保存难度设置
	var difficulty_file = ConfigFile.new()
	difficulty_file.set_value("game", "difficulty", difficulty)
	difficulty_file.save("user://difficulty.cfg")
	
	# 开始游戏
	await get_tree().create_timer(0.3).timeout
	
	# 根据难度选择起始关卡
	var start_scene = ""
	match difficulty:
		1: start_scene = "res://TutorialLevel.tscn"  # 简单难度从教程开始
		2: start_scene = "res://DesertLevel.tscn"    # 普通难度从沙漠开始
		3: start_scene = "res://ForestLevel.tscn"    # 困难难度从森林开始
	
	get_tree().change_scene_to_file(start_scene)

func _on_difficulty_back_button_pressed():
	# 播放按钮音效
	if game_optimizer:
		game_optimizer.play_sound("button_click")
	
	# 隐藏难度选择面板
	$DifficultyPanel.visible = false
